//
//  ExchangeVC.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/2/26.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "ExchangeVC.h"
#import "ExchangeCell.h"
#import "ExcResultView.h"

#import "PrizeTableVC.h"

#import "AppHeader.h"

#import "PrizeModel.h"

@interface ExchangeVC ()

/// 总金豆数量
@property (assign, nonatomic) NSInteger venosaCount;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ExchangeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.venosaCount =163;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 44;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 10)];
    headerView.backgroundColor =tableView.backgroundColor;
    return headerView;
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *footerView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 44)];
    footerView.backgroundColor =[UIColor whiteColor];
    
    UIView *lineView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 1)];
    lineView.backgroundColor =tableView.separatorColor;
    [footerView addSubview:lineView];
    
    UILabel *titleLabel =[[UILabel alloc] initWithFrame:CGRectMake(10, 0, (KScreenWidth-20)/2.0, CGRectGetHeight(footerView.frame))];
    titleLabel.text =@"总兑换";
    [footerView addSubview:titleLabel];
    
    UILabel *contentLabel =[[UILabel alloc] initWithFrame:CGRectMake(10+(KScreenWidth-20)/2.0, 0, (KScreenWidth-20)/2.0, CGRectGetHeight(footerView.frame))];
    contentLabel.textAlignment =NSTextAlignmentRight;
    contentLabel.textColor =APP_DEFAULT_COLOUR;
    contentLabel.text =[NSString stringWithFormat:@"+%ld金币", self.venosaCount];
    [footerView addSubview:contentLabel];
    
    return footerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ExchangeCell *cell =[tableView dequeueReusableCellWithIdentifier:@"ExchangeCell"];
    if (!cell) {
        cell =[ExchangeCell findCellWithIdentifier:@"ExchangeCell"];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    cell.numberLabel.hidden =NO;
    
    PrizeModel *model = self.dataArray[indexPath.row];
    cell.nameLabel.text = [NSString stringWithFormat:@"%@", model.goods.goodsTitle];
    cell.timeLabel.text = [NSString stringWithFormat:@"%@", model.gmtDatetime.length > 16 ? [model.gmtDatetime substringToIndex:16] : model.gmtDatetime];
    [cell.image0View sd_setImageWithURL:[NSURL URLWithString:model.goods.coverUrl] placeholderImage:[UIImage imageNamed:@"quanxi-dianji"]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"点击cell");
}

- (IBAction)trueButtonClick:(UIButton *)sender {
    /// 发请求
#warning 未完成
    [HttpUrl GET:@"myPrize/goodsExchange"dict:@{@"myPrizeId":@(1)} hud:self.view isShow:YES WithSuccessBlock:^(id data) {
        if (BB_isSuccess) {
            [self showAlertViewWithSuccess:YES];
        } else {
            [self showAlertViewWithSuccess:NO];
        }
    }];
    
}

- (void)showAlertViewWithSuccess:(BOOL)isSuccess {
    AlertBgView *alertView =[[AlertBgView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) clickRemove:YES bgColor:[UIColor colorWithWhite:0 alpha:0.6]];
    [alertView showView];
    alertView.centerView_height.constant =310;
    
    ExcResultView *resultView =[ExcResultView loadNib:@"ExcResultView" frame:CGRectMake((KScreenWidth-300)/2.0, 0, 300, alertView.centerView_height.constant)];
    if (isSuccess) {
        resultView.titleLabel.text =@"恭喜老板, 兑换成功";
    } else {
        resultView.titleLabel.text =@"老板, 兑换失败了!";
    }
    [resultView didButtonClickBlock:^(UIButton *button) {
        resultView.viewFrame =CGRectZero;
        [UIView animateWithDuration:0.3 animations:^{
            alertView.alpha =0.5;
            resultView.frame =CGRectMake((KScreenWidth-300)/2.0, (KScreenHeight+300)/2.0, 300, alertView.centerView_height.constant);
        } completion:^(BOOL finished) {
            if (isSuccess) {
                for (UIViewController *VC in self.navigationController.viewControllers) {
                    if ([VC isKindOfClass:NSClassFromString(@"MyPrizeViewController")]) {
                        for (PrizeTableVC *childVC in VC.childViewControllers) {
                            if (childVC.view.superview) {
                                [childVC reloadTableView];
                            }
                        }
                        [self.navigationController popToViewController:VC animated:YES];
                    }
                }
            }
            [resultView removeFromSuperview];
            [alertView removeView];
        }];
    }];
    [alertView.centerView addSubview:resultView];
    
    [resultView startAnimation:UIViewTransitionTypeBottomToTop];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
